package collection01;

import java.util.ArrayList;
import java.util.List;

/**
 * List截取子集的操作
 * List<T> sublist(int start, int end);含头不含尾
 * */
public class ListDemo3 {
    public static void main(String[] args) {
        System.out.println("集合中放入十个元素, 子集获取3到8之间的元素");
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<10; i++){//数组中装十个数
            list.add(i*10);
        }
        System.out.println("list:"+list);//list:[0, 10, 20, 30, 40, 50, 60, 70
        List<Integer> subList = list.subList(3,8);
        System.out.println("subList: "+subList);//subList: [30, 40, 50, 60, 70]

        //将子集的每个元素扩大十倍
        System.out.println("将子集每个元素扩大十倍");
        for(Integer i:subList){
            subList.set(i, subList.get(i)*10);
        }
        System.out.println("subList: "+subList);//subList: [300, 400, 500, 600, 700]
        System.out.println("list: "+list);//list: [0, 10, 20, 300, 400, 500, 600, 700, 80, 90]

        System.out.println("修改list中下标为3的元素");
        list.set(3, 1000);//修改list中下标为3的元素为1000
        System.out.println("list: "+list);//list: [0, 10, 20, 1000, 400, 500, 600, 700, 80, 90]
        //原集合数据改变后,子集也会改变
        System.out.println("subList: "+subList);//subList: [300, 400, 500, 600, 700]

        System.out.println("删除集合中的某个元素");
        ;list.remove(0);
        System.out.println("list: "+list);//list: [10, 20, 1000, 400, 500, 600, 700, 80, 90]
        //原集合长度改变后, 子集讲不能在井陉任何操作了, 若操作则发生异常, 但是可以重新获取子集
        //System.out.println("subList: "+subList);//输出异常
    }
}
